package jmcnet.libcommun.communs
{
	import flexunit.framework.TestCase;
	import flexunit.framework.TestSuite;
	
	import mx.controls.DateField;
	
	import spark.components.DropDownList;
	import spark.components.TextInput;
	import jmcnet.libcommun.communs.helpers.HelperFilter;
	import jmcnet.libcommun.communs.helpers.HelperDate;
	import jmcnet.libcommun.communs.enum.EnumBoolean;
	import jmcnet.libcommun.communs.enum.EnumConditionFiltreDate;

	public class HelperFilterTest extends TestCase
	{
		public function HelperFilterTest(methodName:String=null)
		{
			super(methodName);
		}
		
		public static function suite():TestSuite {
   			var ts:TestSuite = new TestSuite();
   			
   			ts.addTest( new HelperFilterTest( "testHelperFilterEstContenuDans" ) );
			ts.addTest( new HelperFilterTest( "testHelperFilterEstContenuDansTextInput" ) );
			ts.addTest( new HelperFilterTest( "testHelperFilterEstContenuDansEnumBoolean" ) );
			ts.addTest( new HelperFilterTest( "testHelperFilterEstContenuDansDate" ) );
			ts.addTest( new HelperFilterTest( "testHelperFilterEstContenuDansDateAvecCondition" ) );
   			return ts;
   		}
   		
   		public function testHelperFilterEstContenuDans():void {
			assertTrue(HelperFilter.estContenuDans("test","test"));
			assertTrue(HelperFilter.estContenuDans("te","test"));
			assertTrue(HelperFilter.estContenuDans("es","test"));
			assertTrue(HelperFilter.estContenuDans("st","test"));
			assertTrue(HelperFilter.estContenuDans("","test"));
			assertTrue(HelperFilter.estContenuDans(null,"test"));
			assertTrue(HelperFilter.estContenuDans("",null));
			assertTrue(HelperFilter.estContenuDans(null,""));
			assertTrue(HelperFilter.estContenuDans(null,null));
			
			assertTrue(HelperFilter.estContenuDans("Es","TeSt"));
			
			assertFalse(HelperFilter.estContenuDans("ts","test"));
			assertFalse(HelperFilter.estContenuDans("test1","test"));
			assertFalse(HelperFilter.estContenuDans("as",""));
			assertFalse(HelperFilter.estContenuDans("as", null));			
   		}
		
		public function testHelperFilterEstContenuDansTextInput():void {
			var ti:TextInput = new TextInput();
			ti.text = "es";
			assertTrue(HelperFilter.estContenuDansTextInput(ti,"test"));
			ti.text = "test1";
			assertFalse(HelperFilter.estContenuDansTextInput(ti,"test"));
			ti.text = "";
			assertTrue(HelperFilter.estContenuDansTextInput(ti,"test"));
			ti.text = null;
			assertTrue(HelperFilter.estContenuDansTextInput(ti,"test"));
		}
		
		public function testHelperFilterEstContenuDansEnumBoolean():void {
			var lst:DropDownList = new DropDownList();
			lst.dataProvider = EnumBoolean.literalsExtended("Tous");
			
			lst.selectedItem = EnumBoolean.TYPE_Oui;
			assertTrue(HelperFilter.estContenuDansEnumBoolean(lst,true));
			assertFalse(HelperFilter.estContenuDansEnumBoolean(lst,false));
			
			lst.selectedItem = EnumBoolean.TYPE_Non;
			assertTrue(HelperFilter.estContenuDansEnumBoolean(lst,false));
			assertFalse(HelperFilter.estContenuDansEnumBoolean(lst,true));
			
			lst.selectedItem = "Tous";
			assertTrue(HelperFilter.estContenuDansEnumBoolean(lst,false));
			assertTrue(HelperFilter.estContenuDansEnumBoolean(lst,true));
			
			lst.selectedItem = null;
			assertTrue(HelperFilter.estContenuDansEnumBoolean(lst,false));
			assertTrue(HelperFilter.estContenuDansEnumBoolean(lst,true));
			
			lst.selectedItem = "";
			assertTrue(HelperFilter.estContenuDansEnumBoolean(lst,false));
			assertTrue(HelperFilter.estContenuDansEnumBoolean(lst,true));			
		}
		
		public function testHelperFilterEstContenuDansDate():void {
			var dateFltr:DateField = new DateField();
			var dh:HelperDate = new HelperDate(HelperDate.DEFAULT_DATE_FORMAT);
			
			dateFltr.selectedDate = dh.stringToDate("05/11/2011");
			assertTrue(HelperFilter.estContenuDansDate(dateFltr,dh.stringToDate("05/11/2011")));
			assertFalse(HelperFilter.estContenuDansDate(dateFltr,dh.stringToDate("04/11/2011")));
			assertFalse(HelperFilter.estContenuDansDate(dateFltr,dh.stringToDate("06/11/2011")));
			assertFalse(HelperFilter.estContenuDansDate(dateFltr,dh.stringToDate("05/11/2012")));
			assertFalse(HelperFilter.estContenuDansDate(dateFltr,null));
			
			dateFltr.selectedDate = null;
			assertTrue(HelperFilter.estContenuDansDate(dateFltr,dh.stringToDate("05/11/2011")));
			assertTrue(HelperFilter.estContenuDansDate(dateFltr,dh.stringToDate("04/11/2011")));
			assertTrue(HelperFilter.estContenuDansDate(dateFltr,null));
		}
		
		public function testHelperFilterEstContenuDansDateAvecCondition():void {
			var dh:HelperDate = new HelperDate(HelperDate.DEFAULT_DATE_FORMAT);
			
			var dateFltr:DateField = new DateField();
			dateFltr.selectedDate = dh.stringToDate("05/11/2011");
			
			var dateItemInf:Date = dh.stringToDate("04/11/2011");
			var dateItemEgal:Date = dh.stringToDate("05/11/2011");
			var dateItemSupp:Date = dh.stringToDate("06/11/2011");
			
			// Egal
			assertFalse(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Egal, dateFltr, dateItemInf));
			assertTrue(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Egal, dateFltr, dateItemEgal));
			assertFalse(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Egal, dateFltr, dateItemSupp));
			assertFalse(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Egal, dateFltr, null));
			
			// Inferieur
			assertTrue(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Inferieur, dateFltr, dateItemInf));
			assertTrue(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Inferieur, dateFltr, dateItemEgal));
			assertFalse(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Inferieur, dateFltr, dateItemSupp));
			assertFalse(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Inferieur, dateFltr, null));
			
			// Supérieur
			assertFalse(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Superieur, dateFltr, dateItemInf));
			assertTrue(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Superieur, dateFltr, dateItemEgal));
			assertTrue(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Superieur, dateFltr, dateItemSupp));
			assertFalse(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Superieur, dateFltr, null));
			
			dateFltr.selectedDate = null;
			assertTrue(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Egal, dateFltr, null));
			assertTrue(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Inferieur, dateFltr, null));
			assertTrue(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Superieur, dateFltr, null));
			
			assertTrue(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Egal, dateFltr, dateItemSupp));
			assertTrue(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Inferieur, dateFltr, dateItemSupp));
			assertTrue(HelperFilter.estContenuDansDateAvecCondition(EnumConditionFiltreDate.TYPE_Superieur, dateFltr, dateItemSupp));
			
		}
	}
}